package com.xrit.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xrit.constant.MessageConstant;
import com.xrit.exception.CustomException;
import com.xrit.mapper.CheckItemMapper;
import com.xrit.pojo.CheckItem;
import com.xrit.query.QueryPageBean;
import com.xrit.result.PageResult;
import com.xrit.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired //本地调用即可
    private CheckItemMapper checkItemMapper;

    @Override
    public int delete(Integer id) throws CustomException {
        //删除的检查项是否有关联的检查组
        int count = checkItemMapper.selectCountByCheckGroupAndCheckItem(id);
        if(count>0){
            //抛出自定义异常，提示有关联信息
            throw new CustomException(MessageConstant.CHECKITEM_IS_USING);
        }
        int row = checkItemMapper.deleteByPrimaryKey(id);
        return row;
    }

    //添加
    @Override
    public int add(CheckItem checkItem) {
        return checkItemMapper.insertSelective(checkItem);
    }

    /**
     * @param
     * @return
     */
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),true);
        Page<CheckItem> page = checkItemMapper.selectByQueryString(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    //检查项列表实现
    @Override
    public List<CheckItem> findAll() {
        return checkItemMapper.selectByExample(null);
    }
}
